home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3350 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: char vs. unsigned char  ???
  5. Date: Tue, 23 Jan 1996 16:26:48 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3104F028.5100@cmt.lpr.mail.carel.fi>
  8. References: <DLGEKq.1zq@avalon.chinalake.navy.mil>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. sean harvey wrote:
  16. > I recently encorporated some functions from another program compiled and
  17. > linked under an older version (Microsoft C version 6.0) into my program
  18. > (MSVC 7.0).
  19. > [rest snipped]
  20.  
  21. As I first got hold of MSC v7 (Win32 version), I ran into strange problems. After some 
  22. debugging, I found out that there was a bug in MSCv7 (probably there's no more, but I 
  23. think I'll say this anyway...). It seemed that a code where you shift a 8-bit value by 
  24. 8 bits, for instance:
  25.  
  26.     unsigned int x = 0;
  27.     unsigned char c = 'A';
  28.  
  29.     x = ((unsigned int)c) << 8;
  30.  
  31. compiled to:
  32.  
  33.    MOV  AX,0    /* This is placement for x */
  34.    MOV    AL,'A'    /* x = c */
  35.    MOV  AH,AL    /* Compiler optimized: x = c << 8! */
  36.    /* Here, they forgot to zero the lower part (AL) of AX! */
  37.  
  38. The solution to this was to not use literal shift counts (8 here), but to place them 
  39. into a variable:
  40.  
  41.    int  cnt = 8;
  42.  
  43.    ...
  44.    x = ((unsigned int)c) << cnt;
  45.  
  46. This would yield the correct result. So, if your code contains shifts like that, I'd 
  47. check them out by the debugger (what kind of code the compiler emitted).
  48.  
  49. Later,
  50. AriL
  51.  
  52. -- 
  53. All my opinions are mine and mine alone.
  54.